//
//  MineRootViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "MineRootViewController.h"
#import "CyCStandardCell.h"
#import "MineHeaderView.h"
#import "UserModel.h"
#import "H5ViewController.h"
#import "MineRequest.h"
#import "ConnectedDeviceCell.h"
#import "ApplyDrivingCell.h"
#import "MileageTableViewCell.h"
#import "TestRideTableCell.h"
#import "UerCenterModel.h"
#import "CarDataCell.h"
#import "AppDelegate.h"
#import "BaseTabBarController.h"
#import "CommentTestView.h"
#import "CycBLEManager.h"
#import "AgreementView.h"

#import "Cycling-Swift.h"
#import "HealthDataRequest.h"
#import "UserRequest.h"

@interface MineRootViewController ()<UITableViewDataSource , UITableViewDelegate>

@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)NSArray *settings;
@property (nonatomic , strong)MineHeaderView *headerView;
@property (nonatomic, strong) UerCenterModel *model;
@property (strong,nonatomic)NSTimer *timer;
@property (nonatomic, strong) NSString *carId;

@end

@implementation MineRootViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - NavigationBarHeight)style:(UITableViewStyleGrouped)];
        [_tableView registerClass:[CyCStandardCell class] forCellReuseIdentifier:@"CyCStandardCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ConnectedDeviceCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ConnectedDeviceCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ApplyDrivingCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ApplyDrivingCell"];
        [_tableView registerClass:[MileageTableViewCell class] forCellReuseIdentifier:@"MileageTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"TestRideTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TestRideTableCell"];
        [_tableView registerClass:[CarDataCell class] forCellReuseIdentifier:@"CarDataCell"];

        _tableView.backgroundColor = COLOR_APP_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = HEIGHT_CELL48+MARGIN_MIDDLE;
    }
    return _tableView;
}
- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.settings = @[
        @{@"title":CycLocalizedString(@"Cycling history", @"骑行历史") ,@"image":@"icon_mine_history"},
        @{@"title":CycLocalizedString(@"Health insights", @"健康见解"),@"image":@"icon_mine_health"},
    ];
    self.carId = @"";
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.ce_y, SCREEN_W, 160+SafeAreaTopHeight)];
    self.headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 160+SafeAreaTopHeight)];
    [headerView addSubview:self.headerView];
    [self.headerView.carSelectBtn addTarget:self action:@selector(selectCarAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.headerView.carSettingBtn addTarget:self action:@selector(setting) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.headPhotoBtn addTarget:self action:@selector(clickedUserPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.carEidtBtn addTarget:self action:@selector(clickedUserPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.carRecommentBtn addTarget:self action:@selector(clickedRecomment) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableHeaderView = headerView;
    
    [self.view addSubview:self.tableView];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    header.loadingView.color = rgb(70, 221, 162);
    self.tableView.mj_header = header;
    _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(loadData) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    [_timer fire];
    
    [CycBLEManager sharedManager];
    
    [self addShowHaveBikeAlertNotification];
    [self addShowOpenHealthPermissionAlertNotification];
    
    [appDelegate requestFirebasePushTokenOnComplete:^(NSString *pushToken) {
        [MineRequest postDevicePushTokenWithTokenString:pushToken];
    }];
    
//    [self checkCurrentVersion];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    [self loadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}

- (void)updateHeaderView {
    [self.headerView.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.model.userinfo.avatar] placeholderImage:[UIImage imageNamed:@"icon_mine_head"]];
    self.headerView.nameLB.text = self.model.userinfo.name;
    self.headerView.numLB.text = self.model.userinfo.username;
    NSString *levelImageName = self.model.userinfo.level_icon;
    if(![levelImageName hasPrefix:@"http"]) {
        levelImageName = [NSString stringWithFormat:@"%@%@", BaseUrl, levelImageName];
    }
    [self.headerView.carRecommentIMV sd_setImageWithURL:[NSURL URLWithString:levelImageName]];
//    self.headerView.carRecommentLB.text = self.model.userinfo.level_text;
    self.headerView.carRecommentLB.text = @"";
//    self.headerView.carRecommentLB.text = [NSString stringWithFormat:@"%@%@",CycLocalizedString(@"badges", @"勋章"),self.model.userinfo.level];
//    self.headerView.currentCarLB.hidden = self.model.equipment == nil;
//    self.headerView.carSelectBtn.hidden = self.model.equipment == nil;
//    self.headerView.downimview.hidden = self.model.equipment == nil;
    self.headerView.downimview.hidden = YES;
    self.headerView.currentCarLB.text = self.model.equipment.name;
    
    if(self.model.equipment != nil){
        // 判断蓝牙连接状态
        if(![CycBLEManager sharedManager].connected){
            self.headerView.currentCarLB.text = CycLocalizedString(@"Connect trike", @"连接车辆");

            [[CycBLEManager sharedManager] scanAndConnectDeviceWithBleName:self.model.equipment.name success:^(CBPeripheral *peripheral) {
                NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Test];

                NSDictionary *parm = @{
                                       @"flag":@"蓝牙连接成功",
                                       @"content":self.model.equipment.name
                                       };

                [BaseRequest baseGET_New:url cache:NO autoHideLoading:NO parms:parm request:^(id  _Nullable resultObject, NSError * _Nullable error) {
                    
                }];
            } failure:^(NSError *error) {
                NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Test];
                NSDictionary *parm = @{
                                       @"flag":@"蓝牙连接失败",
                                       @"content":self.model.equipment.name
                                       };

                [BaseRequest baseGET_New:url cache:NO autoHideLoading:NO parms:parm request:^(id  _Nullable resultObject, NSError * _Nullable error) {
                }];
            }];
        }
        
    } else {
        self.headerView.currentCarLB.text = CycLocalizedString(@"Connect trike", "连接车辆");
    }

}

- (void)loadData {
    if(appDelegate.applicationDidEnterBackground) {
        return;
    }
    [MineRequest getUserInfoMessageWithEid:self.carId result:^(UerCenterModel * _Nonnull model) {
        [self.tableView.mj_header endRefreshing];
        self.model = model;
        [self updateHeaderView];
        if(model.equipment != nil || model.userinfo.bikestatus == 1){
            self.settings = @[
                @{@"title":CycLocalizedString(@"Test rides", @"试骑"),@"image":@"icon_mine_recomment"},
                @{@"title":CycLocalizedString(@"Cycling history", @"骑行历史") ,@"image":@"icon_mine_history"},
                @{@"title":CycLocalizedString(@"Health insights", @"健康见解"),@"image":@"icon_mine_health"},
                @{@"title":CycLocalizedString(@"Bonus", @"佣金"),@"image":@"icon_mine_bonus"}
            ];
        }else {
            self.settings = @[
                @{@"title":CycLocalizedString(@"Cycling history", @"骑行历史") ,@"image":@"icon_mine_history"},
                @{@"title":CycLocalizedString(@"Health insights", @"健康见解"),@"image":@"icon_mine_health"},
                @{@"title":CycLocalizedString(@"Bonus", @"佣金"),@"image":@"icon_mine_bonus"}
            ];
        }
        [self.tableView reloadData];
    }];
}
- (void)clickedUserPhoto {
    H5ViewController *vc = [[H5ViewController alloc] init];
    vc.url = Cyc_WEBURL_USERINFO;
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)setting {
    H5ViewController *vc = [[H5ViewController alloc] init];
    vc.url = Cyc_WEBURL_SETTINGS;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)selectCarAction {
    H5ViewController *vc = [[H5ViewController alloc] init];
    vc.url = Cyc_WEBURL_CHOOSECAR;
    vc.model = self.model;
    vc.block = ^(NSString *carId) {
        self.carId = carId;
        [self loadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)clickedRecomment {
    H5ViewController *vc = [[H5ViewController alloc] init];
    vc.url = Cyc_WEBURL_XUNZHANG;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ==== datasource代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.settings.count+4;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if([self haveCellBeforeRow:section]) {
        return MARGIN_MIDDLE;
    }
    return CGFLOAT_MIN;
    /*
    if(section == 0){
        return CGFLOAT_MIN;
    }
    if(section == 1) {
        
        if(self.model.equipment == nil) {
            return CGFLOAT_MIN;
        }
        
        if (self.model.tryride != nil) {
            if ([self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1) {
                
            } else {
                return CGFLOAT_MIN;
            }
        }
        
        return [self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1 ? MARGIN_MIDDLE: [self.model.userinfo.tryride intValue] == 1 ? CGFLOAT_MIN : MARGIN_MIDDLE;
    }
    if (section == 2) {
        if (self.model.tryride == nil) {
            return CGFLOAT_MIN;
        }
        return [self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1 ? CGFLOAT_MIN : MARGIN_MIDDLE;
    }
    return MARGIN_MIDDLE;
     */
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section == 0){
        return self.model.equipment == nil ? 0 : 145;
    }
    if(indexPath.section == 1){
        
        if (self.model.tryride != nil) {
            if ([self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1) {
                
            } else {
                return CGFLOAT_MIN;
            }
        }
        
        return [self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1 ? 85 : [self.model.userinfo.tryride intValue] == 1 ? CGFLOAT_MIN : 85;
    }
    if(indexPath.section == 2){
        if (self.model.tryride == nil) {
            return CGFLOAT_MIN;
        }
        return [self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1 ? CGFLOAT_MIN : 280;
    }

    if(indexPath.section == 3){
        return 120;
    }
    return 48;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        if(self.model.equipment == nil){
            ConnectedDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ConnectedDeviceCell" forIndexPath:indexPath];
            if (!cell) {
                cell = [[[NSBundle mainBundle]loadNibNamed:@"ConnectedDeviceCell" owner:self options:nil] lastObject];
            }
            cell.block = ^(NSString * _Nonnull device) {
                H5ViewController *vc = [[H5ViewController alloc] init];
                vc.url = Cyc_WEBURL_CHOOSECAR;
                vc.model = self.model;
                [self.navigationController pushViewController:vc animated:YES];
            };
            cell.clipsToBounds = YES;
            return cell;
        }else {
            CarDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarDataCell" forIndexPath:indexPath];
            cell.model = self.model.equipment;
            cell.carNameLB.text = self.model.equipment.name;
            return cell;
        }
    }
    if(indexPath.section == 1) {
        ApplyDrivingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplyDrivingCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ApplyDrivingCell" owner:self options:nil] lastObject];
        }
        cell.clipsToBounds = YES;
        cell.block = ^(NSString * _Nonnull model) {
            BOOL isAgreemented = [USERDEFAULTS boolForKey:[NSString stringWithFormat:@"kSureAgreement_%ld",(long)UserModel.currentUserInfo.user_id]];
            if (isAgreemented) {
                BaseTabBarController *vc = (BaseTabBarController *)appDelegate.getKeyWindowRootViewController;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SendTryride" object:self];
                [vc selectIndex:4];
            } else {
                [[AgreementView instanceAgreementView] showAgreementViewWithTitles:CycLocalizedString(@"Test ride agreement", @"骑行协议") description:self.model.agreement.str ok:CycLocalizedString(@"Agree", @"同意") okBlock:^{
                    BaseTabBarController *vc = (BaseTabBarController *)appDelegate.getKeyWindowRootViewController;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"SendTryride" object:self];

                    [vc selectIndex:4];
                } cancle:CycLocalizedString(@"Disagree", @"不同意") cancleBlock:^{
                    
                }];
            }
            
        };
        cell.switchBlock = ^(BOOL on) {
            if(on){
                [[AgreementView instanceAgreementView] showAgreementViewWithTitles:CycLocalizedString(@"Test ride agreement", @"骑行协议") description:self.model.agreement.str ok:CycLocalizedString(@"Agree", @"同意") okBlock:^{
                    [MineRequest changeUserInfoWithTryride:on? @"1" :@"0" Result:^(BOOL ret) {
                        if(ret){
                            [self loadData];
                        }
                    }];

                } cancle:CycLocalizedString(@"Disagree", @"不同意") cancleBlock:^{
                }];
            }else {
                [MineRequest changeUserInfoWithTryride:on? @"1" :@"0" Result:^(BOOL ret) {
                    if(ret){
                    }
                }];
            }
        };
        [cell.cellsSwitch setOn4UI:[self.model.userinfo.tryride intValue]==1 animated:YES];
        if (self.model.tryride == nil) {
            cell.hidden = NO;
            if(self.model.equipment == nil && self.model.userinfo.bikestatus != 1){
                cell.titleLB.text = @"你还没有车吗联系客服试驾";
                cell.cellsSwitch.hidden = YES;
                cell.applyBtn.hidden = NO;
            }else {
                cell.titleLB.text = CycLocalizedString(@"Let your car make money for you", @"让你的车为你赚钱");
                cell.cellsSwitch.hidden = NO;
                cell.applyBtn.hidden = YES;
            }
            cell.titleLB.hidden = cell.cellsSwitch.isHidden;
        }else {
            if ([self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1) {
                cell.hidden = NO;
                if(self.model.equipment == nil && self.model.userinfo.bikestatus != 1){
                    cell.titleLB.text = @"你还没有车吗联系客服试驾";
                    cell.cellsSwitch.hidden = YES;
                    cell.applyBtn.hidden = NO;
                }else {
                    cell.titleLB.text = CycLocalizedString(@"Let your car make money for you", @"让你的车为你赚钱");
                    cell.cellsSwitch.hidden = NO;
                    cell.applyBtn.hidden = YES;
                }
                cell.titleLB.hidden = cell.cellsSwitch.isHidden;
            } else {
                cell.hidden = YES;
            }
        }
        return cell;
    }
    if (indexPath.section == 2) {
        TestRideTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestRideTableCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"TestRideTableCell" owner:self options:nil] lastObject];
        }
        cell.acceptBlock = ^(NSString * _Nonnull str) {
            [MineRequest acceptRideWithId:self.model.tryride.id Result:^(BOOL ret) {
                [self loadData];
                if ([self.model.tryride.issqr intValue] == 2) { // 被申请人
                    [self alertViewTitle:CycLocalizedString(@"Note", @"提示") message:CycLocalizedString(@"Please process in Test rides", @"请在推荐中处理") title:CycLocalizedString(@"Confirm", @"确认") block:^{
                    }];
                }
            }];
        };
        cell.refuseBlock = ^(BOOL isRefuse) {
            if (isRefuse) {
                [MineRequest refuseRideWithId:self.model.tryride.id Result:^(BOOL ret) {
                    [self loadData];
                }];
            }else {
                [CommentTestView CommentWith:NSObject.getKeyWindowRootViewController Complection:^(NSString * _Nonnull content) {
                    [MineRequest commentTryrideWithId:self.model.tryride.id content:content Result:^(BOOL ret) {
                        if (ret) {
                            [self showHudTipStr:CycLocalizedString(@"Comment success", @"评论成功")];
                        }
                        [MineRequest endRideWithId:self.model.tryride.id Result:^(BOOL ret) {
                        }];
                    }];
                    [NSObject.getKeyWindowRootViewController gq_removeBgView];
                }];

            }
        };
        cell.model = self.model.tryride;
        
        if (self.model.tryride == nil) {
            cell.hidden = YES;
        }else {
            if ([self.model.tryride.status intValue] == 3 && [self.model.tryride.cstatus intValue] == 3 && [self.model.tryride.isend intValue] == 1) {
                cell.hidden = YES;
            } else {
                cell.hidden = NO;
            }
        }

        return cell;

    }
    if(indexPath.section == 3) {
        MileageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MileageTableViewCell" forIndexPath:indexPath];
        cell.todayArr = self.model.todayArr;
        return cell;
    }
    
    CyCStandardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CyCStandardCell" forIndexPath:indexPath];
    cell.cellType = CycStandardCellTypeDefult;
    [cell setCurrentIndex:0 cellCount:1];
    
    if(self.settings.count > indexPath.section - 4) {
        NSDictionary *setting = self.settings[indexPath.section-4];
        cell.titleLB.text = setting[@"title"];
        cell.imView.image = [UIImage imageNamed:setting[@"image"]];
    }
    
    if(self.settings.count == 4 && indexPath.section == 4) {
        [cell showTryrideNum:self.model.tryridenum];
    } else {
        [cell showTryrideNum:0];
    }
    
    return cell;
}

#pragma mark ==== delegate代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 3) {
        return;
    }
    H5ViewController *vc = [[H5ViewController alloc] init];

    if (indexPath.section == 0) {
        if(self.model.equipment != nil){
            vc.url = [NSString stringWithFormat:@"%@%@%@",BaseUrl,@"#/deviceManagement_new?canshu=",self.model.equipment.id];
            vc.model = self.model;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    NSInteger offset = 0;
    if(self.settings.count == 4) {
        if (indexPath.section == 4) {
            vc.url = Cyc_WEBURL_TRYRIDE;
            [self.navigationController pushViewController:vc animated:YES];
        }
        offset = 1;
    }
    if (indexPath.section == 4 + offset) {
        vc.url = Cyc_WEBURL_RIDEHISTORY;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 5 + offset) {
        vc.url = Cyc_WEBURL_DRIVEHEALTH;
        [HealthDataRequest requestHealthDateRangeWithComplete:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 6 + offset) {
        vc.url = Cyc_WEBURL_BONUS;
        [HealthDataRequest requestHealthDateRangeWithComplete:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

- (BOOL)haveCellBeforeRow:(NSInteger)row
{
    if(row == 0) {
        return NO;
    }
    if([self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:row - 1]] < 1) {
        return NO;
    }
    return YES;
}

@end

/**
  注册完成，弹出是否有车的提醒
 */
@implementation MineRootViewController (YQShowHaveBikeAlert)

- (void)addShowHaveBikeAlertNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showBikeAlertNotification:) name:@"CSShowBikeAlertNotificationKey" object:nil];
}

- (void)showBikeAlertNotification:(NSNotification *)notification
{
    [self showAlertWithTitle:CycLocalizedString(@"Dou you have an emojo trike?", "您拥有我们的设备吗？") message:nil leftTitle:CycLocalizedString(@"No", "否") leftStyle:UIAlertActionStyleDefault leftBlock:^{
        [self updateUsrHaveBikeStatusWithSign:NO];
    } rightTitle:CycLocalizedString(@"Yes", "是") rightStyle:UIAlertActionStyleDestructive rightBlock:^{
        [self updateUsrHaveBikeStatusWithSign:YES];
    }];
}

- (void)updateUsrHaveBikeStatusWithSign:(BOOL)sign
{
    [MineRequest changeUserInfoWithHaveBike:sign result:^(BOOL ret) {
        [self loadData];
    }];
}

+ (void)postShowHaveBikeAlertNotification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CSShowBikeAlertNotificationKey" object:nil];
}

@end

/**
 登陆成功，弹出用户是否需要打开健康权限
 */
@implementation MineRootViewController (YQShowAddHealthPermissionAlert)

- (void)addShowOpenHealthPermissionAlertNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showOpenHealthPermissionAlertNotification:) name:@"CSShowOpenHealthPermissionNotificationKey" object:nil];
}

- (void)showOpenHealthPermissionAlertNotification:(NSNotification *)notification
{
    [self checkShouldShowPermissionPromtOnComplete:^(BOOL shouldShow) {
        if(shouldShow) {
            [self showAlertWithTitle:CycLocalizedString(@"Dou you to connect apple health service?", "您需要连接健康数据吗？") message:nil leftTitle:CycLocalizedString(@"No", "否") leftStyle:UIAlertActionStyleDefault leftBlock:^{
                
            } rightTitle:CycLocalizedString(@"Yes", "是") rightStyle:UIAlertActionStyleDestructive rightBlock:^{
                [self openHealthAppPermissionRequesting];
            }];
        }
    }];
    
}

- (void)openHealthAppPermissionRequesting
{
    [self showHealthPermissionPromotOnComplete:^(BOOL success) {
        if(success) {
            [HealthDataRequest requestHealthDateRangeWithComplete:nil];
        }
    }];
}

+ (void)postShowOpenHealthPermissionAlertNotification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CSShowOpenHealthPermissionNotificationKey" object:nil];
}

@end

/**
 版本更新
 */
@implementation MineRootViewController (VersionManager)

- (void)checkCurrentVersion
{
    
}

@end
